// xc_lshop/api/jtklm/meituan/meituan.js
// var actbill = require('../../../../xc/model/jtklm')
var actbill = require('../../model/extens.js')

var app = getApp();

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    navval: "1",
    nav: [{
        val: "1",
        name: "外卖"
      },
      {
        val: "2",
        name: "果蔬"
      }
    ],
    basicurl: "-1",
    list: {},
  },
  xcloaddata: function (options, config) {


  },
  setconfig(config) {
    if (config['smapiconfig']) {
      if (config['smapiconfig']['basic']['status'] == -1) {

        this.pageback()
      } else {
        var eleui=config['smapiconfig']['eleme']['ui'];
        var navval=-1;
        for(var i in eleui){
          if(eleui[i]['status']==1){
            navval=i;
            break
          }
        }
        this.setData({
          navval:navval,
          eleui:eleui
        })
      }
    } else {
      this.pageback()
    }
   

  },
  afterlogin: function (options, config, userinfo) {
    this.setconfig(config)

    var that = this;
    this.setData({
      member: userinfo
    })
    if (userinfo['member_id'] != 0 && userinfo['memberinfo']) {
      var type = this.data.navval;
      // console.log(type)
     if(this.data.eleui[type]){
      var actid = this.data.eleui[type]['actid'];
      var pid = this.data.eleui[type]['pid'];
      var tpid = this.data.eleui[type]['tpid'];

      this.geturl(actid,pid,tpid, type)

     }
      // console.log(actid)

    }
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
 
  shareposter() {

    var that=this
    // var xcposter = this.selectComponent("#xcposter");
    var eleui = this.data.eleui;
    var type = this.data.navval;
    // var bgimg = eleui[type]['poster']; //"http://md.cohooo.net/attachment/images/2/2021/10/PsmAyyuxmzA4YTwmS7XK4y7979XfI9.png"

    if (!this.data.list[type]) {
     

      app.xc_sys_config.authop.oppower("login").then(function (res) {
        
        if(res['status']==1){
          that.setData({
            member:res['obj']
          })
          app.globalData.member=res['obj']

          var actid = eleui[type].actid
          var pid = eleui[type]['pid'];
          var tpid = eleui[type]['tpid'];

          that.geturl(actid,pid,tpid, type, 'showposter')

        }
      })
    } else {
      this.makeposter()
    }

  },
  makeposter(miniProgramQrCode){
    var xcposter = this.selectComponent("#xcposter");
    var eleui = this.data.eleui;
    var type = this.data.navval;
    var bgimg = eleui[type]['poster']; //"http://md.cohooo.net/attachment/images/2/2021/10/PsmAyyuxmzA4YTwmS7XK4y7979XfI9.png"
    var qrimg =""
    if(miniProgramQrCode){
      qrimg=miniProgramQrCode
    }else if(this.data.list[type] && this.data.list[type]['wx_qrcode_url']){
      qrimg=this.data.list[type]['wx_qrcode_url']; //"http://p0.meituan.net/poiqrcode/74062c815334f1f98f369fd8e6657d9a100811.jpg"
    }
    // console.log("---------------------")
    // console.log(type)
    // console.log(bgimg)
    // console.log(qrimg)
    // console.log("---------------------")

    xcposter.init(type, bgimg, qrimg)
  },
  onClick(e) {
    //  console.log(e)
    this.setData({
      navval: e.detail.name,
    })
  },
  onChange(e) {

    this.setData({
      navval: e.detail.name,
    })

    if (this.data.member['member_id'] != 0 && !this.data.list[e.detail.name]) {
      var type = e.detail.name
      var actid = this.data.eleui[type].actid
      var pid =  this.data.eleui[type]['pid'];
      var tpid =  this.data.eleui[type]['tpid'];

      this.geturl(actid,pid,tpid, type)

    }


  },

  clickbtn(e) {
 
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
    
      if(res['status']==1){
        that.setData({
          member:res['obj']
        })
        app.globalData.member=res['obj']
        var dataset = e.currentTarget.dataset
        console.log(dataset)
        if (dataset.type) {
          var type = dataset.type
          var actid = dataset.actid
          var pid = dataset.pid
          var tpid = dataset.tpid
          
  
          var item = that.data.list[type];
          if (item) {
            var app_path = item['wx_miniprogram_path']
           
            that.navtoxcx(app_path)
          } else {
            that.geturl(actid,pid,tpid, type, 'tonav')
          }
        }
      }
     
     
    })
    // var type= this.data.navval






  },
  navtoxcx(path) {
    var appid = "wxece3a9a4c82f58c9";
    console.log("path")

    console.log(path)
    // if (this.data.navval == 3) {
    //   appid = "wx77af438b3505c00e"
    // }
    wx.navigateToMiniProgram({
      appId: appid,
      path: path,
      fail(e) {
        console.log(e)
      }
    })
  },
  geturl(actid,pid,tpid, type, nav = '') {
    var that = this;
  
    actbill.geturlele(actid,pid,tpid).then(function (res) {
    
      if (res['status'] == 1) {
        if(type){
          var name = 'list[' + type + ']';
          that.setData({
            [name]: res['obj']
          })
        }
        
        if (nav == "tonav") {
          var app_path = res['obj']['wx_miniprogram_path']

          that.navtoxcx(app_path)
        } else if (nav == "showposter") {
          that.makeposter(res['obj']['wx_qrcode_url'])
        }

        console.log(that.data.list)
      } else {
        wx.showModal({
          title: "错误",
          content: res['message'] ? res['message'] : '获取失败'
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})